package com.opmsys.mapper;

import com.opmsys.pojo.entity.Test;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TestMapper {
    /**
     * 新增化验结果
     * @param test
     */
    @Insert("INSERT INTO t_test (estimated_date, actual_date,sampling_volume,spot_test, water_content,  ph,sand_content, well_id, person_number, pump_id) " +
            "VALUES (#{estimatedDate}, #{actualDate}, #{samplingVolume},#{spotTest},#{waterContent}, #{ph}, #{sandContent}, #{wellId}, #{personNumber}, #{pumpId})")
    void insertTest(Test test);

    /**
     * 删除化验结果
     * @param id
     */
    @Delete("DELETE FROM t_test WHERE id = #{id}")
    void deleteTest(@Param("id") Integer id);

    /**
     * 更新化验结果
     * @param test
     */
    @Update("<script>" +
            "UPDATE t_test " +
            "<set>" +
            "<if test='test.estimatedDate != null and test.estimatedDate != \"\" '> estimated_date = #{test.estimatedDate}, </if>" +
            "<if test='test.actualDate != null and test.actualDate != \"\" '> actual_date = #{test.actualDate}, </if>" +
            "<if test='test.samplingVolume != null'> sampling_volume = #{test.samplingVolume}, </if>" +
            "<if test='test.spotTest != null'> spot_test = #{test.spotTest}, </if>" +
            "<if test='test.waterContent != null'> water_content = #{test.waterContent}, </if>" +
            "<if test='test.ph != null'> ph = #{test.ph}, </if>" +
            "<if test='test.sandContent != null'> sand_content = #{test.sandContent}, </if>" +
            "<if test='test.wellId != null'> well_id = #{test.wellId}, </if>" +
            "<if test='test.personNumber != null'> person_number = #{test.personNumber}, </if>" +
            "<if test='test.pumpId != null'> pump_id = #{test.pumpId}, </if>" +
            "</set> " +
            "WHERE id = #{test.id}" +
            "</script>")
    void updateTest(@Param("test") Test test);

    /**
     * 分页查询
     * @param wellId
     * @param personNumber
     * @param pumpId
     * @param estimatedDate
     * @param actualDate
     * @param limit
     * @param offset
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_test " +
            "<where>" +
            "<if test='wellId != null'> AND well_id = #{wellId} </if>" +
            "<if test='personNumber != null'> AND person_number = #{personNumber} </if>" +
            "<if test='pumpId != null'> AND pump_id = #{pumpId}  </if>" +
            "<if test='estimatedDate != null'> AND  estimated_date like concat('%',#{estimatedDate},'%')  </if>" +
            "<if test='actualDate != null'> AND actual_date like concat('%',#{actualDate},'%')  </if>" +
            "</where> " +
            "ORDER BY id " +
            "LIMIT #{limit} OFFSET #{offset}"+
            "</script>")
    List<Test> getTestPageWithConditions(@Param("wellId") Integer wellId,
                                                 @Param("personNumber") Integer personNumber,
                                                 @Param("pumpId") Integer pumpId,
                                                 @Param("estimatedDate") String estimatedDate,
                                                 @Param("actualDate") String actualDate,
                                                 @Param("limit") Integer limit,
                                                 @Param("offset") Integer offset);

    /**
     * 获取查询总数
     * @param wellId
     * @param personNumber
     * @param pumpId
     * @param estimatedDate
     * @param actualDate
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM t_test " +
            "<where>" +
            "<if test='wellId != null'> AND well_id = #{wellId} </if>" +
            "<if test='personNumber != null'> AND person_number = #{personNumber} </if>" +
            "<if test='pumpId != null'> AND pump_id = #{pumpId}  </if>" +
            "<if test='estimatedDate != null'> AND  estimated_date like concat('%',#{estimatedDate},'%')  </if>" +
            "<if test='actualDate != null'> AND actual_date like concat('%',#{actualDate},'%')  </if>" +
            "</where> " +
            "ORDER BY id " +
            "</script>")
    List<Test> getTestListWithConditions(@Param("wellId") Integer wellId,
                                         @Param("personNumber") Integer personNumber,
                                         @Param("pumpId") Integer pumpId,
                                         @Param("estimatedDate") String estimatedDate,
                                         @Param("actualDate") String actualDate);
}